<?php

class CastAddAction extends BaseAction 
{

	public $ades_id;
	public $programme_id;
	public $cast_role_id;
	public $gender;
	public $age;
	public $available_tag_types;
	public $tags;
	public $blog_url;
	public $folders;
	public $status;
	public $created_at;
	public $created_by;
	public $modified_at;
	public $modified_by;

	public $language_id;
	public $cast_name;
	public $nickname;
	public $occupation;
	public $personality;
	public $profile;
	public $history;

	public $ut;
	public function execute(){
		if(count($this->_errors) > 0){
			return 'filter_error';
		}
		$insert_id = $this->add_cast();
		$insert_language_id = $this->add_cast_language($insert_id);
		if (FALSE != $insert_id && $insert_language_id)
		{
			$this->setData('id', $insert_id);
			$this->setData('type', 'cast');
			$this->setData('act', 'add');
			$this->setData('step', 2);
		}

		return "succ";
	}    

	/**
	 * add cast base information
	 */
	private function add_cast()
	{
		$cast = new cast();
		$cast->set($this);
		$cast->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$cast->created_by = CommonUtils::get_user();
		$cast->modified_at = $cast->created_at;
        $cast->modified_by = $cast->created_by;
		$cast->folders = CommonUtils::init_folder('cast',$this);
		$cast->available_tag_types = 1;
		$cast->status = AC::STATUS_NEW;

		$insert_id = Model::$dbo['w']->save($cast);
		if ($insert_id > 0)
		{
			$this->addInfo("cast:create:".$insert_id, "add cast success", json_encode($cast));
			return $insert_id;
		}
		else
		{
			$this->addError("cast:create:".$insert_id, "add cast error", json_encode($cast));
			return false;
		}

	}

	/**
	 * add cast language 
	 */
	private function add_cast_language($cast_id)
	{
		$cast_language = new cast_language();
		$cast_language->set($this);
		$cast_language->cast_id = $cast_id;
		$cast_language->language_id = language::getLanguageID(AC::DEFAULT_LANGUAGE);

		$ret = Model::$dbo['w']->save($cast_language);
		if ($ret >= 0)
		{
			$this->addInfo("cast-language:create:".$cast_id, "add cast language success", json_encode($cast_language));
			return true;
		}
		else
		{
			$this->addError("cast-language:create:".$cast_id, "add cast language error", json_encode($cast_language));
			return false;
		}
	}

}
?>
